import { createRouter, createWebHistory } from 'vue-router'

import AppLogin from '@/views/AppLogin'
import AppLogout from '@/views/AppLogout'
import AppRegister from '@/views/AppRegister'
import AppForget from '@/views/AppForget'
import AppBlog from '@/views/AppBlog'
import BlogHome from '@/views/BlogHome'
import BlogAll from '@/views/BlogAll'
import BlogFrontier from '@/views/BlogFrontier'
import BlogDaily from '@/views/BlogDaily'
import BlogPaper from '@/views/BlogPaper'
import BlogBoard from '@/views/BlogBoard'
import BlogPublish from '@/views/BlogPublish'
import BlogArticle from '@/views/BlogArticle'
import BlogUpdate from '@/views/BlogUpdate'
import BlogFeelings from '@/views/BlogFeelings'
import BlogUserinf from '@/views/BlogUserinf'
import BlogMyinf from '@/views/BlogMyinf'
import MyHome from '@/views/MyHome'
import MyFav from '@/views/MyFav'
import MyPub from '@/views/MyPub'
import MyCom from '@/views/MyCom'
import MyUpd from '@/views/MyUpd'
import AppAdmin from '@/views/AppAdmin'
import AdminHome from '@/views/AdminHome'
import AdminUser from '@/views/AdminUser'
import AdminArticle from '@/views/AdminArticle'
import AdminNotice from '@/views/AdminNotice'
import AdminNoticeadd from '@/views/AdminNoticeadd'
import AdminNoticeUpd from '@/views/AdminNoticeUpd'
import AdminBoard from '@/views/AdminBoard'
import AdminBoardReply from '@/views/AdminBoardReply'
// import AdminMain from '@/views/AdminMain'
// import AdminHome from '@/views/AdminHome'
// import AdminSlog from '@/views/AdminSlog'
// import AdminSlogInf from '@/views/AdminSlogInf'
// import AdminSlogUpdate from '@/views/AdminSlogUpdate'
// import AdminArticle from '@/views/AdminArticle'
// import AdminArticleInf from '@/views/AdminArticleInf'
// import AdminArticleUpdate from '@/views/AdminArticleUpdate'
// import AdminUser from '@/views/AdminUser'
// import AdminUserInf from '@/views/AdminUserInf'
// import AdminUserUpdate from '@/views/AdminUserUpdate'
// import AdminTeam from '@/views/AdminTeam'
// import AdminTeamAdduser from '@/views/AdminTeamAdduser'
// import AdminTeamDeluser from '@/views/AdminTeamDeluser'
// import AdminTeamAdd from '@/views/AdminTeamAdd'
// import AdminTeamUpdate from '@/views/AdminTeamUpdate'
// import AdminQuestion from '@/views/AdminQuestion'
// import AdminQuestionInf from '@/views/AdminQuestionInf'
// import AdminQuestionUpdate from '@/views/AdminQuestionUpdate'
// import AdminQuestionAdd from '@/views/AdminQuestionAdd'
// import AdminComment from '@/views/AdminComment'
// import AdminMesboard from '@/views/AdminMesboard'

// const originalPush = VueRouter.prototype.push
//    VueRouter.prototype.push = function push(location) {
//    return originalPush.call(this, location).catch(err => err)
// }

const router = createRouter({
    // mode: 'history',
    history: createWebHistory(),
    routes:[
        {
            path:'/',
            redirect:'/login'
        },
        {
            path:'/login',
            name:'login',
            component:AppLogin
        },
        {
            path:'/logout',
            name:'logout',
            component:AppLogout
        },
        {
            path:'/register',
            name:'register',
            component:AppRegister
        },
        {
            path:'/forget',
            name:'forget',
            component:AppForget
        },
        {
            path:'/main',
            name:'main',
            redirect: '/home',
            component:AppBlog,
            children: [
                {
                    path:'/home',
                    name:'home',
                    component:BlogHome
                },
                {
                    path:'/blogs',
                    name:'blogs',
                    component:BlogAll
                },
                {
                    path:'/frontier',
                    name:'frontier',
                    component:BlogFrontier
                },
                {
                    path:'/daily',
                    name:'daily',
                    component:BlogDaily
                },
                {
                    path:'/paper',
                    name:'paper',
                    component:BlogPaper
                },
                {
                    path:'/feeling',
                    name:'feeling',
                    component:BlogFeelings
                },
                {
                    path:'/board',
                    name:'board',
                    component:BlogBoard
                },
                {
                    path:'/publish',
                    name:'publish',
                    component:BlogPublish
                },
                {
                    path:'/article',
                    name:'article',
                    component:BlogArticle
                },
                {
                    path:'/update',
                    name:'update',
                    component:BlogUpdate
                },
                {
                    path:'/userinf',
                    name:'userinf',
                    component:BlogUserinf
                },
                {
                    path:'/myinf',
                    name:'myinf',
                    component:BlogMyinf,
                    redirect: '/myhome',
                    children: [
                        {
                            path:'/myhome',
                            name:'myhome',
                            component:MyHome
                        },
                        {
                            path:'/myfav',
                            name:'myfav',
                            component:MyFav
                        },
                        {
                            path:'/mypub',
                            name:'mypub',
                            component:MyPub
                        },
                        {
                            path:'/mycom',
                            name:'mycom',
                            component:MyCom
                        },
                        {
                            path:'/myUpd',
                            name:'myUpd',
                            component:MyUpd
                        },
                    ]
                },
            ]
        },
        {
            path:'/admin/main',
            name:'adminMain',
            component:AppAdmin,
            redirect: '/admin/home',
            children: [
                {
                    path:'/admin/home',
                    name:'adminHome',
                    component:AdminHome
                },
                {
                    path:'/admin/allUser',
                    name:'allUser',
                    component:AdminUser
                },
                {
                    path:'/admin/allArticle',
                    name:'allArticle',
                    component:AdminArticle
                },
                {
                    path:'/admin/allNotice',
                    name:'allNotice',
                    component:AdminNotice
                },
                {
                    path:'/admin/addNotice',
                    name:'addNotice',
                    component:AdminNoticeadd
                },
                {
                    path:'/admin/updNotice',
                    name:'updNotice',
                    component:AdminNoticeUpd
                },
                {
                    path:'/admin/board',
                    name:'adminBoard',
                    component:AdminBoard
                },
                {
                    path:'/admin/boardReply',
                    name:'boardReply',
                    component:AdminBoardReply
                },
            ]
        },
        // {
        //     path:'/login',
        //     name:'login',
        //     component:Login
        // },
        // {
        //     path:'/register',
        //     name:'register',
        //     component:Register
        // },
        // {
        //   path:'/404',
        //   name:'404',
        //   component:NF404
        // },
        // {
        //     path:'/main',
        //     name:'main',
        //     component:Main
        // },
        // {
        //     path:'/Slog',
        //     name:'slog',
        //     component:Slog
        // },
        // {
        //     path:'/solutionlist',
        //     name:'slog',
        //     component:SolutionList
        // },
        // {
        //     path:'/adminlogin',
        //     name:'adminlogin',
        //     component:AdminLogin
        // },{
        //     path:'/newslist',
        //     name:'newslist',
        //     component:NewsList
        // },
        // {
        //     path:'/newsdetail',
        //     name:'newslist',
        //     component:NewsDetail
        // },
        // {
        //     path:'/problemlist',
        //     name:'problemlist',
        //     component:ProblemList
        // },
        // {
        //     path:'/problemdetail',
        //     name:'problemlist',
        //     component:ProblemDetail  
        // },
        // {
        //     path:'/problemsubmission',
        //     name:'problemlist',
        //     component: ProblemSubmission
        // },
        // {
        //     path:'/messagelist',
        //     name:'messagelist',
        //     component:MessageList
        // },
        // {
        //     path:'/slogdetail',
        //     name:'slog',
        //     component:SlogDetail
        // },
        // {
        //     path:'/addslog',
        //     name:'slog',
        //     component:AddSlog
        // },
        // {
        //     path:'/updateslog',
        //     name:'updateslog',
        //     component:UpdateSlog
        // },
        // {
        //     path:'/teamlist',
        //     name:'teamlist',
        //     component:TeamList
        // },
        // {
        //     path:'/globalsearch',
        //     name:'globalsearch',
        //     component:GlobalSearch
        // },
        // {
        //     path:'/qrcode', 
        //     name:'qrcode',
        //     component:QRCode
        // },
        // {
        //     path:'/info',
        //     name:'info',
        //     redirect:'/info/empty',
        //     component:InfoCenter,
        //     children:[
        //         {
        //             path:'selfinfo',
        //             name:'selfinfo',
        //             component:SelfInfo
        //         },
        //         {
        //             path:'selfslog',
        //             name:'selfslog',
        //             component:SelfSlog
        //         },
        //         {
        //             path:'changepic',
        //             name:'changepic',
        //             component:ChangePic
        //         },
        //         {
        //             path:'selfcode',
        //             name:'selfcode',
        //             component:SelfCode
        //         },
        //         {
        //             path:'empty',
        //             name:'empty',
        //             component:Empty
        //         },
        //         {
        //             path:'draft',
        //             name:'draft',
        //             component:Draft
        //         },
        //         {
        //             path:'timeline',
        //             name:'timeline',
        //             component:TimeLine
        //         }
        //     ]
        // },
        // {
        //     path: '/adminMain',
        //     name: 'adminMain',
        //     redirect: '/adminHome',
        //     component: AdminMain,
        //     children: [
        //       {
        //         path: '/adminHome',
        //         name: 'adminHome',
        //         component: AdminHome
        //       },
        //       {
        //         path: '/adminSlog',
        //         name: 'adminSlog',
        //         component: AdminSlog
        //       },
        //       {
        //         path: '/adminSlogInf',
        //         name: 'adminSlogInf',
        //         component: AdminSlogInf
        //       },
        //       {
        //         path: '/adminSlogUpdate',
        //         name: 'adminSlogUpdate',
        //         component: AdminSlogUpdate
        //       },
        //       {
        //         path: '/adminArticle',
        //         name: 'adminArticle',
        //         component: AdminArticle
        //       },
        //       {
        //         path: '/adminArticleUpdate',
        //         name: 'adminArticleUpdate',
        //         component: AdminArticleUpdate
        //       },
        //       {
        //         path: '/adminArticleInf',
        //         name: 'adminArticleInf',
        //         component: AdminArticleInf
        //       },
        //       {
        //         path: '/adminUser',
        //         name: 'adminUser',
        //         component: AdminUser
        //       },
        //       {
        //         path: '/adminUserInf',
        //         name: 'adminUserInf',
        //         component: AdminUserInf
        //       },
        //       {
        //         path: '/adminUserUpdate',
        //         name: 'adminUserUpdate',
        //         component: AdminUserUpdate
        //       },
        //       {
        //         path: '/adminTeam',
        //         name: 'adminTeam',
        //         component: AdminTeam
        //       },
        //       {
        //         path: '/adminTeamAdduser',
        //         name: 'adminTeamAdduser',
        //         component: AdminTeamAdduser
        //       },
        //       {
        //         path: '/adminTeamDeluser',
        //         name: 'adminTeamDeluser',
        //         component: AdminTeamDeluser
        //       },
        //       {
        //         path: '/adminTeamAdd',
        //         name: 'adminTeamAdd',
        //         component: AdminTeamAdd
        //       },
        //       {
        //         path: '/adminTeamUpdate',
        //         name: 'adminTeamUpdate',
        //         component: AdminTeamUpdate
        //       },
        //       {
        //         path: '/adminQuestion',
        //         name: 'adminQuestion',
        //         component: AdminQuestion
        //       },
        //       {
        //         path: '/adminQuestionInf',
        //         name: 'adminQuestionInf',
        //         component: AdminQuestionInf
        //       },
        //       {
        //         path: '/adminQuestionAdd',
        //         name: 'adminQuestionAdd',
        //         component: AdminQuestionAdd
        //       },
        //       {
        //         path: '/adminQuestionUpdate',
        //         name: 'adminQuestionUpdate',
        //         component: AdminQuestionUpdate
        //       },
        //       {
        //         path: '/adminComment',
        //         name: 'adminComment',
        //         component: AdminComment
        //       },
        //       {
        //         path: '/adminMesboard',
        //         name: 'adminMesboard',
        //         component: AdminMesboard
        //       }
        //     ]
        // }
    ]
})

router.beforeEach((to, form, next) => {
  const role = window.localStorage.getItem('role')
  if ((to.path === '/' || to.path === '/login' || to.path === '/logout' || to.path === '/register' || to.path === '/forget') && (!role)) {
    next()
  } else if (!role) {
    next('/login')
  } else if (to.path.match(/^\/admin.*/)) {
    if (role.match('USER')) next('/home')
    else if (to.path === '/admin/allUser' && !role.match('SUPER')) next('/admin/home')
    else next()
  } else {
    next()
  }
})

export default router

